package com.student.work.service.impl;

import com.student.work.common.BaseResponse;
import com.student.work.common.ResultCodeEnum;
import com.student.work.mapper.UserProfileMapper;
import com.student.work.mapper.UserRegisterInfoMapper;
import com.student.work.model.UserProfile;
import com.student.work.model.UserRegisterInfo;
import com.student.work.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date 2020/2/22 13:22
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserRegisterInfoMapper registerInfoMapper;

    @Autowired
    UserProfileMapper userProfileMapper;

    @Override
    public BaseResponse<Map<String, Object>> signUp(UserRegisterInfo info) {
        log.info("用户注册 ---> 开始");
        Map<String, Object> result = new HashMap<>(16);
        // 先判断是否存在
        UserRegisterInfo user = registerInfoMapper.getUser(info.getUserName(), info.getMobile(), null);
        if(user != null){
            if(info.getUserName().equals(user.getUserName())){
                return BaseResponse.result(ResultCodeEnum.EXIST_NAME, result);
            }
            if(info.getMobile().equals(user.getMobile())){
                return BaseResponse.result(ResultCodeEnum.EXIST_MOBILE, result);
            }
        }

        // 保存数据
        long userId = System.currentTimeMillis();
        info.setUserId(userId);
        Date date = new Date();
        info.setGmtCreate(date);
        info.setGmtModified(date);
        int insert = registerInfoMapper.insert(info);

        if(insert > 0){
            result.put("userId", userId);
            result.put("role", info.getRole());
            log.info("用户注册 ---> 成功，useId：" + userId);
            return BaseResponse.result(ResultCodeEnum.SUCCESS, result);
        }
        return BaseResponse.result(ResultCodeEnum.ERROR, result);
    }

    @Override
    public BaseResponse<Map<String, Object>> login(UserRegisterInfo info) {
        log.info("用户登录 ---> 开始");
        Map<String, Object> result = new HashMap<>(16);
        UserRegisterInfo user = registerInfoMapper.getUser(info.getUserName(), null, info.getPassword());
        if(user != null){
            result.put("userId", user.getUserId());
            result.put("role", user.getRole());
            result.put("userName", user.getUserName());
            log.info("用户登录 ---> 成功");
            return BaseResponse.result(ResultCodeEnum.SUCCESS, result);
        }
        return BaseResponse.result(ResultCodeEnum.WRONG_USER, result);
    }

    @Override
    public BaseResponse<Map<String, Object>> createProfile(UserProfile profile){
        log.info("用户信息完善 ---> 开始");
        Map<String, Object> result = new HashMap<>(16);

        Long userId = profile.getUserId();
        if(userId == null){
            return BaseResponse.result(ResultCodeEnum.UN_LOGIN, result);
        }

        int res;
        long profileId;
        // 先查数据库是否存在。存在，更新数据；不存在，插入数据。
        UserProfile exist = userProfileMapper.selectByUserId(userId);
        if(exist != null){
            profile.setGmtModified(new Date());
            res = userProfileMapper.update(profile);
            profileId = exist.getProfileId();
        } else {
            profileId = System.currentTimeMillis();
            profile.setProfileId(profileId);
            Date date = new Date();
            profile.setGmtCreate(date);
            profile.setGmtModified(date);
            res = userProfileMapper.insert(profile);
        }

        if(res > 0){
            result.put("profileId", profileId);
            log.info("用户注册 ---> 成功，profileId：" + profileId);
            return BaseResponse.result(ResultCodeEnum.SUCCESS, result);
        }
        return BaseResponse.result(ResultCodeEnum.ERROR, result);
    }

    @Override
    public BaseResponse<UserProfile> getProfile(Long userId){
        log.info("获取用户信息 ---> 开始，userId：" + userId);
        if(userId == null){
            return BaseResponse.result(ResultCodeEnum.ERROR, null);
        }
        UserProfile profile = userProfileMapper.selectByUserId(userId);
        if (profile != null){
            log.info("获取用户信息 ---> 完成");
            return BaseResponse.result(ResultCodeEnum.SUCCESS, profile);
        }
        return BaseResponse.result(ResultCodeEnum.ERROR, null);
    }

    @Override
    public BaseResponse<Map<String, Object>> userList(int pageNum, int pageSize){
        Map<String, Object> result = new HashMap<>(16);
        if (pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == 0) {
            pageSize = 5;
        }
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);

        pageNum = (pageNum - 1) * pageSize;

        int total = registerInfoMapper.getCount();
        List<UserRegisterInfo> list = registerInfoMapper.getList(pageNum, pageSize);

        result.put("total", total);
        result.put("data", list);
        log.info("获取用户列表 --> 完成，总数：" + total);
        return BaseResponse.result(ResultCodeEnum.SUCCESS, result);
    }

    @Override
    public BaseResponse<Map<String, Object>> deleteUser(UserRegisterInfo info){
        Map<String, Object> result = new HashMap<>(16);
        Long userId = info.getUserId();
        if(userId != null){
            int delete = registerInfoMapper.delete(userId);
            result.put("delete", delete);
        }
        return BaseResponse.result(ResultCodeEnum.SUCCESS, result);
    }
}
